package com.ruoyi.buss.grid.sh.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.buss.grid.sh.domain.DataGtgsh;
import com.ruoyi.buss.grid.sh.domain.DataWgShsurvey;
import com.ruoyi.buss.grid.sh.service.IDataGtgshService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 网格商户Controller
 * 
 * @author zjh
 * @date 2021-07-15
 */
@RestController
@RequestMapping("/gridSh")
public class DataGtgshController extends BaseController
{
	@Autowired
	private TokenService tokenService;
	
    @Autowired
    private IDataGtgshService dataGtgshService;

    
    /**
	 * 	网格商户业绩列表
	 *
	 * @return
	 */
    @PreAuthorize(hasPermi = "buss:gridSh:queryGridSh")
    @GetMapping("/queryGridSh")
    public AjaxResult queryGridSh(DataGtgsh dataGtgsh)
	{
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		Map<String,Object> map = new HashMap<>();
		List<DataGtgsh> list = dataGtgshService.dataWgShList(dataGtgsh,userInfo);
		DataWgShsurvey dataWgShsurvey =dataGtgshService.dataWgShsurvey(dataGtgsh,userInfo);
		map.put("pageCommonVO",getDataTable(list));//支行商户列表
		map.put("dataWgShsurvey", dataWgShsurvey); //支行概况
		return AjaxResult.success(map);
	}
    
    /**
     * 包片商户列表
     * @param dataGtgsh
     * @return
     */
    @PreAuthorize(hasPermi = "buss:gridSh:queryGtgshList")
    @GetMapping("/queryGtgshList")
    public TableDataInfo queryGtgshList(DataGtgsh dataGtgsh)
	{
		LoginUser userInfo = tokenService.getLoginUser();
		startPage();
		List<DataGtgsh> list = dataGtgshService.queryGtgshList(dataGtgsh,userInfo);
		return getDataTable(list);
	}
	/**
	 * 查询个体工商户
	 * @param idCard
	 * @return
	 */
    @PreAuthorize(hasPermi = "buss:gridSh:itemGtgsh")
    @GetMapping("/itemGtgsh")
    public AjaxResult itemGtgsh(String idCard)
	{
		DataGtgsh list = dataGtgshService.itemGtgsh(idCard);
		return AjaxResult.success(list);
	}
    
    /**
     * 新增网格商户
     */
    @PreAuthorize(hasPermi = "buss:gridSh:addGtgsh")
    @Log(title = "网格商户", businessType = BusinessType.INSERT)
    @PostMapping("/addGtgsh")
    public AjaxResult add(@RequestBody DataGtgsh dataGtgsh)
    {
    	LoginUser userInfo = tokenService.getLoginUser();
        return dataGtgshService.insertDataGtgsh(dataGtgsh,userInfo);
    }
    
    /**
	 * 	删除个体工商户  逻辑删除
	 * @param gtgsh
	 * @return
	 */
    @PreAuthorize(hasPermi = "buss:gridSh:deleteGtgsh")
    @Log(title = "网格商户", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/deleteGtgsh")
    public AjaxResult deleteGtgsh( @RequestBody DataGtgsh gtgsh)
    {
        return dataGtgshService.deleteGtgsh(gtgsh);
    }
    
    /**
     * 修改个体工商户信息
     */
    @PreAuthorize(hasPermi = "buss:gridSh:updateGtgsh")
    @Log(title = "网格商户", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateGtgsh")
    public AjaxResult edit(@RequestBody DataGtgsh dataGtgsh)
    {	
        return dataGtgshService.updateDataGtgsh(dataGtgsh);
    }
    
    /**
     * 修改网格商户信息管户
     */
    @PreAuthorize(hasPermi = "buss:gridSh:updateGtgshOrg")
    @Log(title = "网格商户", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateGtgshOrg")
    public AjaxResult updateGtgshOrg(@RequestBody DataGtgsh dataGtgsh)
    {
    	LoginUser userInfo = tokenService.getLoginUser();
        return dataGtgshService.updateGtgshOrg(dataGtgsh,userInfo);
    }


    /**
     *  网格商户提交网格营销任务
     */
    @PreAuthorize(hasPermi = "buss:gridSh:shTask")
    @PostMapping("/shTask")
    public AjaxResult shTask(@RequestBody DataGtgsh dataGtgsh)
    {
    	if (dataGtgsh.getId() != null && "0".equals(dataGtgsh.getId())) //long 默认为0
		{
			return AjaxResult.error("参数错误");
		}
    	LoginUser userInfo = tokenService.getLoginUser();
		return dataGtgshService.shTask(dataGtgsh, userInfo);
    }
    
    /**
	 * APP网格概况折线图
	 */
  //  @PreAuthorize(hasPermi = "buss:gridSh:wgshAppBiYear")
	@GetMapping(value = "/wgshAppBiYear")
	public TableDataInfo wgcjAppBiYear()
	{
    	LoginUser userInfo = tokenService.getLoginUser();
    	startPage();
    	List<DataWgShsurvey> list = dataGtgshService.wgshAppBiYear(userInfo);
		return getDataTable(list);
	}
	
	/**
	 * APP网格概况折线图
	 */
   // @PreAuthorize(hasPermi = "buss:gridSh:wgshAppBiJd")
        @GetMapping(value = "/wgshAppBiJd")
	public TableDataInfo wgsqAppBiJd()
	{
    	LoginUser userInfo = tokenService.getLoginUser();
    	startPage();
    	List<DataWgShsurvey> list = dataGtgshService.wgshAppBiJd(userInfo);
		return getDataTable(list);
	}
    
	/**
	 * APP网格概况折线图
	 */
   // @PreAuthorize(hasPermi = "buss:gridSh:wgshAppBiMonth")
	@GetMapping(value = "/wgshAppBiMonth")
	public TableDataInfo wgshAppBiMonth()
	{
    	LoginUser userInfo = tokenService.getLoginUser();
    	startPage();
    	List<DataWgShsurvey> list = dataGtgshService.wgshAppBiMonth(userInfo);
		return getDataTable(list);
	}
    
    
    
    /**
     * 查询网格商户列表
     */
    @PreAuthorize(hasPermi = "buss:gridSh:list")
    @GetMapping("/list")
    public TableDataInfo list(DataGtgsh dataGtgsh)
    {
        startPage();
        List<DataGtgsh> list = dataGtgshService.selectDataGtgshList(dataGtgsh);
        return getDataTable(list);
    }

    /**
     * 删除网格商户
     */
    @PreAuthorize(hasPermi = "buss:gridSh:remove")
    @Log(title = "网格商户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dataGtgshService.deleteDataGtgshByIds(ids));
    }
    
    /**
     * 导出网格商户列表
     */
    @PreAuthorize(hasPermi = "buss:gridSh:export")
    @Log(title = "网格商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataGtgsh dataGtgsh) throws IOException
    {
        List<DataGtgsh> list = dataGtgshService.selectDataGtgshList(dataGtgsh);
        ExcelUtil<DataGtgsh> util = new ExcelUtil<DataGtgsh>(DataGtgsh.class);
        util.exportExcel(response, list, "网格商户数据");
    }

    /**
     * 获取网格商户详细信息
     */
    @PreAuthorize(hasPermi = "buss:gridSh:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dataGtgshService.selectDataGtgshById(id));
    }

}
